package com.yxhl.zoume.utils.tcp;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.yxhl.protobuf.TcpRequest;
import com.yxhl.zoume.ZouMeApplication;
import com.yxhl.zoume.data.tcp.OnChannelListener;
import com.yxhl.zoume.data.tcp.TcpClient;
import com.yxhl.zoume.di.component.trip.DaggerTripComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.di.module.trip.TripModule;
import com.yxhl.zoume.utils.LOG;

import javax.inject.Inject;

/**
 * Created by vhawk on 16/4/20.
 * 本服务用于发送TCP长链接
 */
public class TcpService extends Service {

    private static final String TAG = "TcpService";

    @Inject
    TcpClient client;

    private NetworkChangeReceiver mNetworkChangeReceiver;
    private TcpClientController mTcpClientController;

    @Override
    public void onCreate() {
        super.onCreate();
        LOG.e(TAG, "onCreate");
        initializeInjector();
        registerNetWorkReceiver();
        onRxBusEvent();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        mTcpClientController = new TcpClientController();
        return mTcpClientController;
    }

    private void onRxBusEvent() {


    }

    @Override
    public boolean onUnbind(Intent intent) {
        LOG.e(TAG, "onUnbind");
        mTcpClientController.closeChannel();
        return super.onUnbind(intent);
    }

    private void registerNetWorkReceiver() {
        if (mNetworkChangeReceiver == null) {
            mNetworkChangeReceiver = new NetworkChangeReceiver();
            registerReceiver(mNetworkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
    }

    private void unRegisterNetWorkReceiver() {
        if (mNetworkChangeReceiver != null) {
            unregisterReceiver(mNetworkChangeReceiver);
        }
    }

    @Override
    public void onDestroy() {
        LOG.e(TAG, "onDestroy");
        unRegisterNetWorkReceiver();
        super.onDestroy();
    }

    private void initializeInjector() {
        DaggerTripComponent
                .builder()
                .appComponent(ZouMeApplication.application.getAppComponent())
                .activityModule(new ActivityModule(this))
                .tripModule(new TripModule())
                .build()
                .inject(this);
    }

    public class TcpClientController extends Binder {

        /**
         * 判断TCP链接是否成功
         *
         * @return
         */
        public boolean isConnect() {
            return client.isConnect();
        }

        /**
         * 关闭TCP通道
         */
        public void closeChannel() {
            client.close();
        }

        /**
         * 向TCP通道写数据
         *
         * @param tcpRequest
         */
        public void sendTcpRequest(TcpRequest tcpRequest) {
            client.sendRequest(tcpRequest);
        }

        /**
         * 开启tcp长链接
         */
        public void startTcpConnection(OnChannelListener onChannelListener) {
            TcpClient.isHandClose = false;
            TcpClient.isConnect = false;
            client.connect(onChannelListener);
        }


    }

    //    网络监听，断网到重连网络（程序已进入，就会调用)
    protected class NetworkChangeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //从无网络到有网络连接时运行该广播回调方法
            if (!client.isConnect()) {
                String action = intent.getAction();
                if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

                    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                    if (networkInfo != null && networkInfo.isAvailable()) {
                        int type = networkInfo.getType();
                        switch (type) {
                            case ConnectivityManager.TYPE_WIFI:

                                break;
                            case ConnectivityManager.TYPE_MOBILE:

                                break;
                        }
                    }
                    //没有网络链接
                    else {

                    }

                }
            }

        }
    }


}
